package com.tsunami.handler;


import com.tsunami.base.R;
import com.tsunami.exception.BizException;
import com.tsunami.exception.code.ExceptionCode;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Classname GlobalException
 * @Description TODO
 * @Date 2021-06-10 11:28
 * @Created by lx
 */
@ControllerAdvice
public class GlobalExceptionHandler {
    /**
     * 全局异常
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public R error(Exception e){
        e.printStackTrace();
        return R.error();
    }
    @ExceptionHandler(ArithmeticException.class)
    @ResponseBody
    public R error(ArithmeticException e){
        e.printStackTrace();
        return R.error()
                .code(ExceptionCode.ARITHMETICEXCEPTION.getCode())
                .msg(ExceptionCode.ARITHMETICEXCEPTION.getMsg());
    }

    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    @ResponseBody
    public R error(HttpRequestMethodNotSupportedException e){
        e.printStackTrace();
        return R.error().exCode(ExceptionCode.METHODEXCEPTION);
    }

    @ExceptionHandler(BizException.class)
    @ResponseBody
    public R error(BizException e){
        e.printStackTrace();
        return R.error()
                .code(e.getCode())
                .msg(e.getMessage());
    }

}
